/*****************************************************
*                                                                          
*               COPYRIGHT (c) 2015-2017 Hargic Corporation                 
*                         All Rights Reserved                              
*                                                                          
* The source code contained or described herein and all documents          
* related to the source code ("Material") are owned by Hargic            
* Corporation or its licensors.  Title to the Material remains             
* with Hargic Corporation or its suppliers and licensors.                  
*                                                                          
* The Material is protected by worldwide copyright and trade secret        
* laws and treaty provisions. No part of the Material may be used,         
* copied, reproduced, modified, published, uploaded, posted, transmitted,  
* distributed, or disclosed in any way except in accordance with the       
* applicable license agreement.                                            
*                                                                          
* No license under any patent, copyright, trade secret or other            
* intellectual property right is granted to or conferred upon you by       
* disclosure or delivery of the Materials, either expressly, by            
* implication, inducement, estoppel, except in accordance with the         
* applicable license agreement.                                            
*                                                                          
* Unless otherwise agreed by Hargic in writing, you may not remove or      
* alter this notice or any other notice embedded in Materials by Hargic    
* or Hargic's suppliers or licensors in any way.                           
*                                                                          
** Author: shengda.huang
** Date: 2018-05-18
*
*****************************************************/

/***** Include files ********************************/
#include <stdio.h>
#include <string.h>
#include "rf433.h"
#include "rf433_master.h"

/***** Defines **************************************/

#ifndef BIG_ENDIAN
#define LITTLE_ENDIAN
#endif

#define SWAP_16BIT(a)		(((a >> 8) & 0x00ff) | ((a << 8) & 0xff00))

/***** Define structure *****************************/

/***** Define constant ******************************/

/***** Define global ********************************/

unsigned char master_hostid[3];

/***** Define Prototype of functions ****************/

/**
 * @brief
 * @param  None
 * @retval None
 */
void rf433_init(void)
{
#ifdef RF433_MASTER
	master_hostid[0] = 'M';
	master_hostid[1] = '0';
	master_hostid[2] = '1';
#endif
}

int rf433_create_pkg(unsigned char *buff, unsigned int maxlen, unsigned short addr, unsigned short seq, unsigned short msgtype, unsigned int bodylen)
{
	rf433_header_t *header = (rf433_header_t *)buff;

	if (maxlen < (sizeof(rf433_header_t) + bodylen))
	{
		printf("%s: maxlen %d short than msg(%d + %d)\n", __func__, maxlen, sizeof(rf433_header_t), bodylen);
		return -1;
	}
#ifndef LITTLE_ENDIAN
	header->head 	= SWAP_16BIT(RF433_HEAD_ST);
	header->addr 	= SWAP_16BIT(addr);
	header->seq 	= SWAP_16BIT(seq);
	header->msgtype = SWAP_16BIT(msgtype);
	header->crc 	= SWAP_16BIT(0);
#else
	header->head 	= RF433_HEAD_ST;
	header->addr 	= addr;
	header->seq 	= seq;
	header->msgtype = msgtype;
	header->crc 	= 0;
#endif
	
	memcpy(header->hostid, master_hostid, sizeof(master_hostid));
	header->bodylen	= bodylen;

	return (sizeof(rf433_header_t) + bodylen);
}

int rf433_msg_handler(unsigned char *buff, unsigned int len)
{
	rf433_header_t *header = (rf433_header_t *)buff;

	printf("%s: enter\n", __func__);

	if (header->head != RF433_HEAD_ST)
	{
		printf("%s: head %04x err\n", __func__, header->head);
		return -1;
	}

	if (len != (header->bodylen + sizeof(rf433_header_t)))
	{
		printf("%s: len %d not match bodylen %d + sizeof_head %d\n", __func__, len, header->bodylen, sizeof(rf433_header_t));
		return -3;
	}

#ifdef RF433_MASTER
	printf("%s: recv addr %x msgtype %d\n", __func__, header->addr, header->msgtype);
	if (header->addr == RF433_INVALID_ADDR && header->msgtype == RF433_MSG_SLAVE_REG_REQ)
	{
		switch (header->msgtype)
		{
			case RF433_MSG_SLAVE_REG_REQ: 
				rf433_register_req_handler(buff, len);
				break;
		}
	}
	else
	{
		if (memcmp(header->hostid, master_hostid, sizeof(master_hostid)) != 0)
		{
			printf("%s: hostid %02x %02x %02x err\n", __func__, header->hostid[0], header->hostid[1], header->hostid[2]);
			return -2;
		}

		if (rf433_find_slv(header->addr) < 0)
		{
			printf("%s: not find slave addr %x\n", __func__, header->addr);
			return -4;
		}

		switch (header->msgtype)
		{
			case RF433_MSG_SLAVE_REG_REQ: 
				rf433_register_req_handler(buff, len);
				break;
			case RF433_MSG_SLAVE_REPORT_SWITCH_REQ:
				rf433_register_report_switch_handler(buff, len);
				break;
		}
	}
#else
	if (header->addr != local_addr)
	{
		printf("%s: slave addr %x error\n", __func__, header->addr);
		return -4;
	}

	printf("%s: recv msgtype %x\n", __func__, header->msgtype);
	switch (header->msgtype)
	{
		case RF433_MSG_SLAVE_REQ_RSP:
			// rf433_register_resp_handler(buff, len);
			break;
	}
#endif

	return 0;
}

/* End of file */



